// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: linchuan

#pragma once

#include <memory>
#include <string>
#include <unordered_map>

#include "coin2/exchange/base/order/order.h"
#include "coin2/exchange/binance_base/api_util/auth_key.h"
#include "coin2/exchange/binance_base/api_util/rate_tracker.h"
#include "coin2/exchange/order/binance.pb.h"

namespace coin2::exchange::binance::api_order {

enum BinanceOrderSubTopicId : int {
  REST_DEPOSIT = 10000,
  REST_WITHDRAWAL = 10001,
  DEPOSIT_SUBACCOUNT = 10002,
  WITHDRAW_SUBACCOUNT = 10003,
  REST_FLEXIBLE_PRODUCT_POSITION = 10004,
  REST_LOCKED_PRODUCT_POSITION = 10005
};

}  // namespace coin2::exchange::binance::api_order

class BinanceNativePrivateClient : public coin2::exchange::base::order::NativePrivateClient {
 public:
  using RestParams = coin2::exchange::order::BinanceRestParams;
  using NativeClientConfig = coin2::exchange::order::OrderConnectionConfig;
  using RestQueryProto = coin2::exchange::base::order::RestQueryProto;
  using BinanceRateTracker = coin2::exchange::binance::api_util::BinanceRateTracker;
  using BinanceOrderSubTopicId = coin2::exchange::binance::api_order::BinanceOrderSubTopicId;
  using AsioHttpContextList = std::list<std::shared_ptr<AsioHttpContext>>;

 public:
  std::shared_ptr<AsioHttpContext> SubmitOrderExecution(const OrderExecutionProto& order);
  std::shared_ptr<AsioHttpContext> SubmitRestQuery(const RestQueryProto& query);

 public:
  explicit BinanceNativePrivateClient(
      const MarketExchangeApi& mea,
      const NativeClientConfig& config);

  std::shared_ptr<AsioHttpContext> QueryAccountBalance(const RestParams& params);
  std::shared_ptr<AsioHttpContext> QueryFlexibleProductPosition(const RestParams& params);
  std::shared_ptr<AsioHttpContext> QueryLockedProductPosition(const RestParams& params);
  std::shared_ptr<AsioHttpContext> QueryMarginCrossedAccountBalance(const RestParams& params);
  std::shared_ptr<AsioHttpContext> QueryMarginIsolatedAccountBalance(const RestParams& params);
  std::shared_ptr<AsioHttpContext> QueryOpenOrders(const RestParams& params);
  std::shared_ptr<AsioHttpContext> QueryAllOrders(const RestParams& params);
  std::shared_ptr<AsioHttpContext> QueryTrades(const RestParams& params);
  std::shared_ptr<AsioHttpContext> PlaceOrder(const RestParams& params);
  std::shared_ptr<AsioHttpContext> CancelOrder(const RestParams& params);
  std::shared_ptr<AsioHttpContext> GenListenKey(bool is_init);
  std::shared_ptr<AsioHttpContext> ExtendListenKey();
  std::shared_ptr<AsioHttpContext> DeleteListenKey();

  std::shared_ptr<AsioHttpContext> QueryDeposit(const RestParams& params);
  std::shared_ptr<AsioHttpContext> QueryWithdrawal(const RestParams& params);
  std::shared_ptr<AsioHttpContext> QueryTransfer(const RestParams& params);

  std::shared_ptr<AsioHttpContext> PlaceOrderNorm(const OrderExecution&) override;
  std::shared_ptr<AsioHttpContext> CancelOrderNorm(const OrderExecution&) override;
  void Prepare() override;
  AsioHttpContextList SubmitRestQueryList(const RestQueryProto& query);
  RestParams GetRestOrderParams(const OrderSpec& order_spec, int64_t proc_order_id);
  RestParams GetRestCancelParams(const IOrderContext& order_context);

  std::string DebugString() const;

  std::string GetCurrMilliseconds() const {
    return std::to_string(GetCurrentTimestamp() / 1000000);
  }
  BaseRestAuth* GetAuth() const override;

  std::string GetFirstKeyEmail();

 private:
  void SetBindIp(const std::string&);

 private:
  int64_t start_time_;
  std::string listen_key_;
  std::unordered_map<std::string, int32_t> api_block_info_;
  std::unordered_map<std::string, int32_t> api_req_info_;
  bool is_first_query_account_balance_ = true;
  std::unordered_map<std::string, int64_t> query_open_orders_ts_map_;
};
